<template>
  <el-tabs v-model="activeName" v-loading="loading">
    <el-tab-pane label="规则配置" name="first">
      <el-card class="box-card">
				<div v-if="notice !== ''" class="uni-notice">{{notice}}</div>
        <el-form ref="gameParamTmp" :model="gameParamTmp" label-width="120px">
          <el-row :gutter="15">
            <el-col :span="22">
              <el-form-item label="上期开奖时间" prop="endDate" :rules="[
                ]">
                <el-input v-model="gameParamTmp.endDate" placeholder="" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="开奖周期" prop="period" :rules="[
                  {required: true, message:'开奖周期不能为空', trigger:'blur'}
                ]">
                <el-input v-model="gameParamTmp.period" placeholder="单位：天"></el-input>
              </el-form-item>
              <el-form-item label="每期次数" prop="count" :rules="[
                  {required: true, message:'每期次数不能为空', trigger:'blur'}
                ]">
                <el-input v-model="gameParamTmp.count" placeholder="会员每期可玩几次"></el-input>
              </el-form-item>
              <el-form-item label="开局积分" prop="cost" :rules="[
                  {required: true, message:'开局积分不能为空', trigger:'blur'}
                ]">
                <el-input v-model="gameParamTmp.cost" placeholder="每玩一次消化多少积分"></el-input>
              </el-form-item>
              <el-form-item label="名次奖励" prop="reward" :rules="[
                  {required: true, message:'名次奖励不能存在空数据', trigger:'blur'}
                ]">
                <el-button type="success" icon="el-icon-plus" circle @click="addReward"
                  v-if="gameParamTmp.reward.length === 0"></el-button>
                <div v-for="(reward,index) in gameParamTmp.reward">
                  <el-col :span="4">
                    <el-input v-model="reward.start" placeholder="起始名次" style="margin: 3px 0;"></el-input>
                  </el-col>
                  <el-col :span="2" style="text-align: center; padding: 0;">
                    ~
                  </el-col>
                  <el-col :span="4">
                    <el-input v-model="reward.end" placeholder="结束名次" style="margin: 3px 0;"></el-input>
                  </el-col>
                  <el-col :span="4" style="text-align: center; padding: 0;">
                    奖励
                  </el-col>
                  <el-col :span="4">
                    <el-input v-model="reward.reward" placeholder="奖励积分数" style="margin: 3px 0;"></el-input>
                  </el-col>
                  <el-col :span="6">
                    <el-button type="danger" icon="el-icon-minus" circle @click="deleteReward(index)"
                      style="margin: 3px;"></el-button>
                    <el-button type="success" icon="el-icon-plus" circle
                      v-if="index === gameParamTmp.reward.length-1" @click="addReward"
                      style="margin: 3px;"></el-button>
                  </el-col>
                </div>
              </el-form-item>
							<el-form-item label="玩法说明" prop="howplay" :rules="[
							    {required: true, message:'玩法说明不能为空', trigger:'blur'}
							  ]">
							  <el-input type="textarea" rows="4" v-model="gameParamTmp.howplay" placeholder="玩法说明"></el-input>
							</el-form-item>
							<el-form-item label="规则说明" prop="rule" :rules="[
							    {required: true, message:'规则说明不能为空', trigger:'blur'}
							  ]">
							  <el-input type="textarea" rows="8" v-model="gameParamTmp.rule" placeholder="规则说明"></el-input>
							</el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item>
                <el-button type="primary" @click="doSubmitBack()">保存</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </el-tab-pane>
  </el-tabs>
</template>
<script>
  export default {
    data() {
      return {
        activeName: 'first',
        loading: false,
				notice: '',
        gameParamTmp: {
          endDate: '',
          period: '1',
          count: '3',
          cost: '10',
          reward: [],
					howplay: '',
					rule: ''
        }
      }
    },
    activated() {
      this.refreshList()
    },
    methods: {
      // 获取数据列表
      refreshList() {
        this.$http({
          url: '/smis/config/jfConfig/queryByKey',
          method: 'post',
          params: {
            'keyStr': "gameParamTmp"
          }
        }).then(({
          data
        }) => {
          if (data && data.success) {
            console.log(data)
            if(data.data) {
              this.gameParamTmp = data.data
            }
          }
        })
				
				this.$http({
				  url: '/smis/api/haiding/queryConfigEffect',
				  method: 'post',
				  params: {
				  }
				}).then(({
				  data
				}) => {
				  if (data && data.success) {
						this.notice = data.msg
				  }
				})
      },
      addReward() {
        this.gameParamTmp.reward.push({
          "start": "",
          "end": "",
          "reward": ""
        })
      },
      deleteReward(index) {
        this.gameParamTmp.reward.splice(index, 1);
      },
      isIntNum(val) {
        var regPos = /^\d+(\.\d+)?$/; //非负浮点数
        var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
        if (regPos.test(val) || regNeg.test(val)) {
          return true;
        } else {
          return false;
        }
      },
      doSubmitBack() {
        for (var i = 0; i < this.gameParamTmp.reward.length; i++) {
          if (!this.isIntNum(this.gameParamTmp.reward[i].start)) {
            this.$message.error("请填满名次奖励中所有输入项，并去除非数字字符")
            return
          }
          if (!this.isIntNum(this.gameParamTmp.reward[i].end)) {
            this.$message.error("请填满名次奖励中所有输入项，并去除非数字字符")
            return
          }
          if (!this.isIntNum(this.gameParamTmp.reward[i].reward)) {
            this.$message.error("请填满名次奖励中所有输入项，并去除非数字字符")
            return
          }
        }

        this.loading = true
        this.$http({
          url: `/smis/config/jfConfig/saveConfig`,
          method: 'post',
          data: {
            keyStr: "gameParamTmp",
            valueStr: JSON.stringify(this.gameParamTmp)
          }
        }).then(({
          data
        }) => {
          if (data && data.success) {
            this.$message.success(data.msg)
            this.refreshList()
          }
          this.loading = false
        })
      }
    }
  }
</script>
<style type="text/css" scoped="">
	.uni-notice {
		font-size: 14px;
		color: #F5222D;
		line-height: 30px;
		margin-bottom: 20px;
		padding-left: 25px;
		box-sizing: border-box;
	}
</style>
